package project.background.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import fld.background.service.UserService;
import fld.background.system.service.impl.BaseServiceImpl;
import fld.utils.Common;
import fld.utils.DataGrid;
import fld.utils.Page;
import fld.utils.Result;
import project.background.dto.ProductTypeDto;
import project.background.entity.ProductType;

/**
 * 企业产品分类
 * @author yc
 * 2019年1月11日
 */
@Service
public class ProductTypeService extends BaseServiceImpl<ProductType,Long>{
	
	@Autowired
	private UserService userService;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 查询全部
	 * @param tmap
	 * @return
	 * @author yc
	 * @date 2018年12月7日
	 */
	public List<Map<String,Object>> queryAll(Map<String,Object> tmap){
		if("".equals(Common.nullToEmpty(tmap.get("companyId")))) {
			tmap.put("companyId", userService.getCurUser().getCompanyId());
		}else {
			tmap.put("companyId", Long.parseLong(tmap.get("companyId").toString()));
		}
		List<ProductType> list = query(ProductTypeDto.class, tmap);
		List<Map<String,Object>> res = new ArrayList<Map<String,Object>>();
		for (ProductType t : list) {
			Map<String,Object> m = new HashMap<String,Object>();
			m.put("code", t.getCode());
			m.put("text", t.getName());
			res.add(m);
		}
		return res; 
	}
	
	/**
	 * 查询分页
	 * @param tmap
	 * @return
	 */
	public DataGrid grid(Map<String,Object> tmap){
		if(!"".equals(Common.nullToEmpty(tmap.get("name")))){
			tmap.put("name","%"+tmap.get("name")+"%");
		}
		if("".equals(Common.nullToEmpty(tmap.get("companyId")))) {
			tmap.put("companyId", userService.getCurUser().getCompanyId());
		}else {
			tmap.put("companyId", Long.parseLong(tmap.get("companyId").toString()));
		}
		Page<ProductType> page = queryForPage(ProductTypeDto.class, tmap);
		List<Map<String,Object>> rows = new ArrayList<Map<String,Object>>();
		List<ProductType> res = page.getRows();
		for (ProductType t : res) {
			Map<String,Object> m = new HashMap<String,Object>();
			m.put("id", t.getId());
			m.put("createTime", null == t.getCreateTime()?"":sdf.format(t.getCreateTime()));
			m.put("name", t.getName());
			m.put("code", t.getCode());
			m.put("companyId", t.getCompanyId());
			m.put("zindex", t.getZindex());
			rows.add(m);
		}
		DataGrid dg = new DataGrid();
		dg.setTotal(page.getTotal());
		dg.setRows(rows);
		dg.setTotalPage(page.getPageCount());
		return dg;
	}
	
	/**
	 * 获取详情
	 * @param id
	 * @return
	 */
	public Map<String,Object> getInfo(Long id){
		ProductType t = getDao().get(id);
		Map<String,Object> m = new HashMap<String,Object>();
		if(t != null){
			m.put("id", t.getId());
			m.put("createTime", null == t.getCreateTime()?"":sdf.format(t.getCreateTime()));
			m.put("name", t.getName());
			m.put("code", t.getCode());
			m.put("zindex", t.getZindex());
			m.put("companyId", t.getCompanyId());
		}
		return m;
	}
	
	/**
	 * 新增/修改
	 * @param t
	 */
	public Result saveObj(ProductType t) {
		if("".equals(Common.nullToEmpty(t.getId()))){
			//新增
			Long companyId = userService.getCurUser().getCompanyId();
			if(null  == companyId) {
				return new Result(false,"没有操作权限");
			}else {
				t.setCompanyId(companyId);
			}
			ProductType pt = queryByCodeAndCompanyId(t.getCode(), companyId);
			if(null != pt) {
				return new Result(false,"该编码已存在");
			}
			save(t);
		}else{
			
			ProductType old = getDao().get(t.getId());
			
			ProductType pt = queryByCodeAndCompanyId(t.getCode(), old.getCompanyId());
			if(null != pt) {
				if(pt.getId() != old.getId()) {
					return new Result(false,"该编码已存在");
				}
			}
			
			old.setName(t.getName());
			old.setCode(t.getCode());
			old.setZindex(t.getZindex());
			save(old);
		}
		return new Result(true);
	}
	
	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	public Result deleteById(Long id){
		ProductType t = getDao().get(id);
		if(null != t) {
			t.setIsdeleted(true);
			save(t);
		}
		return new Result(true);
	}
	
	/**
	 * 
	 * @param name
	 * @param companyId
	 * @return
	 * 2019年1月11日
	 * @author yc
	 */
	public String queryNameByCodeAndCompanyId(String code,Long companyId){
		ProductType t = queryByCodeAndCompanyId(code,companyId);
		return null == t ? "" : t.getName();
	}
	
	/**
	 * 
	 * @param name
	 * @param companyId
	 * @return
	 * 2019年1月11日
	 * @author yc
	 */
	public ProductType queryByCodeAndCompanyId(String code,Long companyId){
		code = Common.nullToEmpty(code);
		ProductType t = getDao().getByHQL("from ProductType where code = (?) and companyId = (?) and isdeleted = 0", code,companyId);
		return t;
	}
}
